home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Bitmap / Sources / BmpSel.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  13.2 KB  |  501 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                BmpSel.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef BMPSEL_H
  14. #include "BmpSel.h"
  15. #endif
  16.  
  17. #ifndef BMPPART_H
  18. #include "BmpPart.h"
  19. #endif
  20.  
  21. #ifndef BMPFACET_H
  22. #include "BmpFacet.h"
  23. #endif
  24.  
  25. // ----- Frameworks Includes -----
  26.  
  27. #ifndef FWUTIL_H
  28. #include "FWUtil.h"
  29. #endif
  30.  
  31. #ifndef FWFRAME_H
  32. #include "FWFrame.h"
  33. #endif
  34.  
  35. #ifndef FWFACET_H
  36. #include "FWFacet.h"
  37. #endif
  38.  
  39. // ----- Graphic Includes -----
  40.  
  41. #ifndef FWGC_H
  42. #include "FWGC.h"
  43. #endif
  44.  
  45. #ifndef FWRECSHP_H
  46. #include "FWRecShp.h"
  47. #endif
  48.  
  49. #ifndef FWPICCSHP_H
  50. #include "FWPicShp.h"
  51. #endif
  52.  
  53. // ----- Foundation Includes -----
  54.  
  55. #ifndef FWSUSTRM_H
  56. #include <FWSUStrm.h>
  57. #endif
  58.  
  59. #ifndef FWMEMMGR_H
  60. #include <FWMemMgr.h>
  61. #endif
  62.  
  63. // ----- OpenDoc Includes -----
  64.  
  65. #ifndef _SHAPE_
  66. #include <Shape.h>
  67. #endif
  68.  
  69. #ifndef _DRAGDROP_
  70. #include <DragDrop.h>
  71. #endif
  72.  
  73. #ifndef _XMPSESSN_
  74. #include <XMPSessM.h>
  75. #endif
  76.  
  77. #ifndef _STDPROPS_
  78. #include <StdProps.h>
  79. #endif
  80.  
  81. #ifndef _STDTYPES_
  82. #include <StdTypes.h>
  83. #endif
  84.  
  85. #ifndef _TRNSFORM_
  86. #include <Trnsform.h>
  87. #endif
  88.  
  89. #ifndef _TRANSLAT_
  90. #include <Translat.h>
  91. #endif
  92.  
  93. // ----- Macintosh Includes -----
  94.  
  95. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  96. #include <Drag.h>
  97. #endif
  98.  
  99. #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
  100. #include <ToolUtils.h>
  101. #endif
  102.  
  103. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  104. #include <math routines.h>
  105. #endif
  106.  
  107. #pragma segment bitmappart
  108.  
  109. //========================================================================================
  110. //    •• class CBitmapSelection
  111. //========================================================================================
  112.  
  113. //---------------------------------------------------------------------------------------
  114. //    • CBitmapSelection::CBitmapSelection
  115. //---------------------------------------------------------------------------------------
  116.  
  117. CBitmapSelection::CBitmapSelection() :
  118.     fBitmapPart(NULL),
  119.     fHasSelection(FALSE),
  120.     fAntPattern(FW_kAntPat)
  121. {
  122. }
  123.  
  124. //---------------------------------------------------------------------------------------
  125. //    • CBitmapSelection::InitBitmapSelection
  126. //---------------------------------------------------------------------------------------
  127.  
  128. void CBitmapSelection::InitBitmapSelection(CBitmapPart* thePart)
  129. {
  130.     InitSelection(thePart, FALSE, FALSE);
  131.     
  132.     fBitmapPart = thePart;
  133. }
  134.  
  135. //---------------------------------------------------------------------------------------
  136. //    • CBitmapSelection::~CBitmapSelection
  137. //---------------------------------------------------------------------------------------
  138.  
  139. CBitmapSelection::~CBitmapSelection()
  140. {    
  141. }
  142.  
  143. //---------------------------------------------------------------------------------------
  144. //    • CBitmapSelection::CloseSelection
  145. //---------------------------------------------------------------------------------------
  146.  
  147. void CBitmapSelection::CloseSelection()
  148. {
  149.     if (fHasSelection)
  150.     {
  151.         DrawAnts(fBitmapPart->GetActiveFrame());        // Erase ants
  152.         fHasSelection = FALSE;
  153.     }
  154. }
  155.  
  156. //---------------------------------------------------------------------------------------
  157. //    • CBitmapSelection::DoClear
  158. //---------------------------------------------------------------------------------------
  159. // We don't support clear selection. Just return false because the document
  160. // hasn't changed.
  161.  
  162. FW_Boolean CBitmapSelection::DoClear()
  163. {
  164.     return FALSE;
  165. }
  166.  
  167. //---------------------------------------------------------------------------------------
  168. //    • CBitmapSelection::SelectAll
  169. //---------------------------------------------------------------------------------------
  170.  
  171. void CBitmapSelection::SelectAll()
  172. {
  173.     // ----- Close the selection if there is one -----
  174.     CloseSelection();
  175.     
  176.     // ----- Get the active frame and it's shape -----
  177.     FW_CRect rect;
  178.     FW_CFrame* activeFrame = fBitmapPart->GetActiveFrame();
  179.     activeFrame->GetFrameShapeBounds(&rect);
  180.     SetSelectRect(rect);
  181.     
  182.     // ----- Draw the first ants -----
  183.     DrawAnts(activeFrame);
  184. }
  185.  
  186. //---------------------------------------------------------------------------------------
  187. //    • CBitmapSelection::IsEmpty
  188. //---------------------------------------------------------------------------------------
  189.  
  190. FW_Boolean CBitmapSelection::IsEmpty() const
  191. {
  192.     return !fHasSelection;
  193. }
  194.  
  195. //---------------------------------------------------------------------------------------
  196. //    • CBitmapSelection::ExternalizeSelection
  197. //---------------------------------------------------------------------------------------
  198.  
  199. void CBitmapSelection::ExternalizeSelection(XMPStorageUnit* storageUnit, FW_CFrame* commandFrame, XMPCloneKind cloneKind)
  200. {
  201. FW_UNUSED(commandFrame);
  202. FW_UNUSED(cloneKind);
  203.  
  204.     // ----- Create the content property-----
  205.     storageUnit->AddProperty(kXMPPropContents)->AddValue(fBitmapPart->GetContentPropertyValueType());
  206.     
  207.     FW_CBitmapShape selectionShape = fBitmapPart->GetBitmapShape()->GetBitmapParts(fSelectRect);
  208.  
  209.     FW_CStorageUnitSink sink(storageUnit);
  210.     FW_CWritableStream stream(&sink);
  211.     selectionShape->Flatten(stream);
  212. }
  213.  
  214. //---------------------------------------------------------------------------------------
  215. //    • CBitmapSelection::InternalizeSelection
  216. //---------------------------------------------------------------------------------------
  217.  
  218. FW_Boolean CBitmapSelection::InternalizeSelection(XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
  219. {
  220.     FW_UNUSED(cloneKind);
  221.  
  222.     FW_Boolean internalized = FALSE;
  223.  
  224.     XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
  225.     XMPType PICTType = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
  226.  
  227.     // ----- If there is a property content -----
  228.     if (storageUnit->Exists(kXMPPropContents, PICTType, 0))
  229.     {
  230.         storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, PICTType, (XMPValueIndex)0, kXMPPosUndefined);
  231.         InternalizePICT(storageUnit);
  232.         internalized = TRUE;
  233.     }
  234.     else if (storageUnit->Exists(kXMPPropContents, kBitmapPartKind, 0))
  235.     {
  236.         storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, kBitmapPartKind, (XMPValueIndex)0, kXMPPosUndefined);
  237.         InternalizeBitmap(storageUnit);
  238.         internalized = TRUE;
  239.     }
  240.  
  241.     if (internalized)
  242.         fBitmapPart->InvalidateAllFrames(kNoPresentation, NULL, NULL);
  243.  
  244.     return internalized;
  245. }
  246.  
  247. //---------------------------------------------------------------------------------------
  248. //    • CBitmapSelection::InternalizePICT
  249. //---------------------------------------------------------------------------------------
  250.  
  251. void CBitmapSelection::InternalizePICT(XMPStorageUnit* storageUnit)
  252. {
  253.     unsigned long pictSize = storageUnit->GetSize();
  254.     
  255.     FW_PlatformHandle handle = FW_CMemoryManager::AllocateSystemHandle(pictSize);
  256.     FW_CMemoryManager::LockSystemHandle(handle);
  257.     storageUnit->GetValue(pictSize, (XMPValue)(*handle));
  258.     FW_CMemoryManager::UnlockSystemHandle(handle);
  259.     
  260.     FW_CRect rect = (*((FW_PlatformPict)handle))->picFrame;
  261.     rect.Place(0,0);
  262.     FW_CBitmapShape selection((FW_PlatformPict)handle, rect);    
  263.     fBitmapPart->SetBitmap(selection);
  264.     
  265.     fBitmapPart->InvalidateAllFrames(kNoPresentation, NULL, NULL);
  266. }
  267.  
  268. //---------------------------------------------------------------------------------------
  269. //    • CBitmapSelection::InternalizeBitmap
  270. //---------------------------------------------------------------------------------------
  271.  
  272. void CBitmapSelection::InternalizeBitmap(XMPStorageUnit* storageUnit)
  273. {
  274.     FW_CBitmapShape bitmapShape = fBitmapPart->GetBitmapShape();
  275.     FW_CStorageUnitSink sink(storageUnit);
  276.     FW_CReadableStream stream(&sink);
  277.     bitmapShape->Unflatten(stream);
  278. }
  279.  
  280. //---------------------------------------------------------------------------------------
  281. //    • CBitmapSelection::GetSelectionShape
  282. //---------------------------------------------------------------------------------------
  283. // Return the shape of the selection. This will be added to the clipboard or
  284. // drag and drop storage unit.
  285.  
  286. XMPShape* CBitmapSelection::GetSelectionShape()
  287. {
  288.     FW_CRect rect(fSelectRect);
  289.     rect.Offset(-rect.left, -rect.top);
  290.     return ::NewXMPShape(rect);
  291. }
  292.  
  293. //---------------------------------------------------------------------------------------
  294. //    • CBitmapSelection::MoveAnts
  295. //---------------------------------------------------------------------------------------
  296.  
  297. void CBitmapSelection::MoveAnts()
  298. {
  299.     FW_CFrame *frame = fBitmapPart->GetActiveFrame();
  300.     if (frame != NULL)
  301.     {
  302.         FW_CGraphicContext gc(*frame->GetActiveFacet());
  303.     
  304.         DoDrawAnts(&gc);
  305.     
  306.         fAntPattern.ShiftRight();
  307.             
  308.         DoDrawAnts(&gc);
  309.     }
  310. }
  311.  
  312. //---------------------------------------------------------------------------------------
  313. //    • CBitmapSelection::DrawAnts
  314. //---------------------------------------------------------------------------------------
  315. // Draw ants in all facets of the given frame.
  316.  
  317. void CBitmapSelection::DrawAnts(FW_CFrame* frame)
  318. {
  319.     if (frame != NULL)
  320.     {
  321.         FW_CFrameFacetIterator ite(frame);
  322.         while (!ite.IsDone())
  323.         {
  324.             FW_CFacet* facet = ite.CurrentItem();
  325.             
  326.             {
  327.                 FW_CGraphicContext gc(*facet);    
  328.                 DoDrawAnts(&gc);
  329.             }
  330.             
  331.             ite.Next();
  332.         }
  333.     }
  334. }
  335.  
  336. //---------------------------------------------------------------------------------------
  337. //    • CBitmapSelection::DoDrawAnts
  338. //---------------------------------------------------------------------------------------
  339.  
  340. void CBitmapSelection::DoDrawAnts(FW_CGraphicContext* gc)
  341. {
  342.     CBitmapFacet* facet = (CBitmapFacet*)FW_CFacet::XMPtoFWFacet(gc->GetXMPFacet());
  343.     FW_CPoint ratio = facet->GetZoomRatio();
  344.     
  345.     FW_CRect rect;
  346.     GetSelectRectWithZoomFactor(facet, rect);
  347.  
  348.     FW_CRectShape rectShape(rect);
  349.     rectShape->SetPattern(fAntPattern);
  350.     rectShape->SetTransferMode(FW_kXOr);
  351.     rectShape->SetShapeFill(FW_kFramed);
  352.     rectShape->Draw(gc);
  353. }
  354.  
  355. //-------------------------------------------------------------------------
  356. // CBitmapSelection::Track
  357. //-------------------------------------------------------------------------
  358. // Tracker for the selection. This should be replace later by a tracker object.
  359.  
  360. FW_Boolean CBitmapSelection::Track(CBitmapFacet* facet, const FW_CPoint& anchorPoint, XMPEventData event, FW_CRect& rect)
  361. {
  362.     if (!::WaitMouseMoved(event->where))
  363.         return FALSE;
  364.  
  365.     FW_CRect maxRect;
  366.     facet->GetFrame()->GetFrameShapeBounds(&maxRect);
  367.     
  368.     FW_CGraphicContext gc(*facet);
  369.     
  370.     FW_CRectShape rectShape(rect);
  371.     rectShape->SetPattern(FW_kAntPat);
  372.     rectShape->SetTransferMode(FW_kXOr);
  373.     rectShape->SetShapeFill(FW_kFramed);
  374.         
  375.     FW_CPoint currentLoc;
  376.     FW_CPoint prevLoc = anchorPoint;
  377.     
  378.     FW_Boolean stillDown = TRUE;
  379.     FW_Boolean erase = FALSE;
  380.     
  381.     while (stillDown)
  382.     {
  383.         FW_SPlatformPoint qdLoc;
  384.         ::GetMouse(&qdLoc);
  385.         currentLoc = qdLoc;
  386.         
  387.         // ----- Adjust for the size of the cursor
  388.         if (anchorPoint.x < currentLoc.x)
  389.             currentLoc.x++;
  390.         if (anchorPoint.y < currentLoc.y)
  391.             currentLoc.y++;
  392.         
  393.         // ----- Test if moved    
  394.         if (prevLoc != currentLoc)
  395.         {
  396.             if (erase)
  397.                 rectShape->Draw(&gc);
  398.             
  399.             // ----- Calcul rectangle -----
  400.             if (anchorPoint.x < currentLoc.x)
  401.             {
  402.                 rect.left = anchorPoint.x;
  403.                 rect.right = currentLoc.x;
  404.             }
  405.             else
  406.             {
  407.                 rect.left = currentLoc.x;
  408.                 rect.right = anchorPoint.x + ff(1);
  409.             }
  410.             
  411.             if (anchorPoint.y < currentLoc.y)
  412.             {
  413.                 rect.top = anchorPoint.y;
  414.                 rect.bottom = currentLoc.y;
  415.             }
  416.             else
  417.             {
  418.                 rect.top = currentLoc.y;
  419.                 rect.bottom = anchorPoint.y + ff(1);
  420.             }
  421.             
  422.             rect &= maxRect;
  423.             
  424.             rectShape->SetRectangle(rect);
  425.             
  426.             // ----- Draw it -----
  427.             rectShape->Draw(&gc);
  428.             
  429.             prevLoc = currentLoc;
  430.             erase = TRUE;
  431.         }
  432.         
  433.         stillDown = ::StillDown();
  434.     }
  435.     
  436.     if (erase)
  437.         rectShape->Draw(&gc);
  438.     
  439.     // ----- Apply the zoom ratio ----
  440.     FW_CPoint ratio = facet->GetZoomRatio();
  441.     rect.left = ::FixDiv(rect.left, ratio.x);
  442.     rect.right = ::FixDiv(rect.right, ratio.x);
  443.     rect.top = ::FixDiv(rect.top, ratio.y);
  444.     rect.bottom = ::FixDiv(rect.bottom, ratio.y);
  445.  
  446.     return (currentLoc != anchorPoint);
  447. }
  448.  
  449. //-------------------------------------------------------------------------
  450. // CBitmapSelection::InSelection
  451. //-------------------------------------------------------------------------
  452. // Test if a point is in the selection.
  453.  
  454. FW_Boolean CBitmapSelection::InSelection(const FW_CPoint& where) const
  455. {
  456.     if (IsEmpty())
  457.         return FALSE;
  458.     
  459.     return fSelectRect.Contains(where);
  460.     
  461. }
  462.  
  463. //-------------------------------------------------------------------------
  464. // CBitmapSelection::CalcDragRgn
  465. //-------------------------------------------------------------------------
  466. // Calculate the region for the Drag and Drop Manager.
  467.  
  468. XMPRgnHandle CBitmapSelection::CalcDragRgn(FW_CFacet* facet)
  469. {
  470. FW_UNUSED(facet);
  471.  
  472.     FW_CRect selectRect;
  473.     GetSelectRectWithZoomFactor((CBitmapFacet*)facet, selectRect);
  474.     
  475.     FW_SPlatformRect rect = selectRect;
  476.     
  477.     XMPRgnHandle dragRgn = ::NewRgn();
  478.     XMPRgnHandle tempRgn = ::NewRgn();
  479.     
  480.     ::RectRgn(dragRgn, &rect);
  481.     ::CopyRgn(dragRgn, tempRgn);
  482.     ::InsetRgn(tempRgn, 1, 1);
  483.     ::DiffRgn(dragRgn, tempRgn, dragRgn);
  484.     
  485.     ::DisposeRgn(tempRgn);
  486.         
  487.     return dragRgn;
  488. }
  489.  
  490. //-------------------------------------------------------------------------
  491. // CBitmapSelection::GetSelectRectWithZoomFactor
  492. //-------------------------------------------------------------------------
  493.  
  494. void CBitmapSelection::GetSelectRectWithZoomFactor(CBitmapFacet* facet, FW_CRect& selectRect)
  495. {
  496.     FW_CPoint ratio = facet->GetZoomRatio();
  497.     selectRect.Set(::FixMul(fSelectRect.left, ratio.x),
  498.                     ::FixMul(fSelectRect.top, ratio.y),
  499.                     ::FixMul(fSelectRect.right, ratio.x),
  500.                     ::FixMul(fSelectRect.bottom, ratio.y));
  501. }